﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formProductIncome : FormMDIChildTemplate
    {
        public formProductIncome()
        {
            InitializeComponent();
        }

        private void formMaterialProductIncome_Load(object sender, EventArgs e)
        { FillInvoice(); root.TranslateGrid(gridInvoice); }

        private void FillInvoice()
        {
            var invoiceIncomes = context.InvoiceIncomes.OrderByDescending(x => x.DateCreate);
            invoiceIncomeBindingSource.DataSource = invoiceIncomes;
            var firms = context.Firms.Where(x => x.FirmTypeID == 2);
            firmBindingSource.DataSource = firms;
            var storeHouses = context.StoreHouses.Select(x => x);
            storeHouseBindingSource.DataSource = storeHouses;
        }

        private void gridInvoice_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            string value = e.Value as string;
            string columnName = gridInvoice.Columns[e.ColumnIndex].Name.ToUpper();
            if (columnName.Contains("FIRM"))
            {
                Firm firm = (from x in firmBindingSource.OfType<Firm>()
                             where x.ToString() == value
                             select x).FirstOrDefault();
                e.Value = firm;
            }
            else if (columnName.Contains("STOREHOUSE"))
            {
                StoreHouse storeHouse = (from x in storeHouseBindingSource.OfType<StoreHouse>()
                                         where x.ToString() == value
                                         select x).FirstOrDefault();
                e.Value = storeHouse;
            }

            e.ParsingApplied = true;
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            this.ActiveControl = buttonSelect;
            root.Submit(); FillInvoice();
        }


        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (gridInvoice.Rows.Count > 0)
            {
                InvoiceIncome invoiceIncome = (InvoiceIncome)invoiceIncomeBindingSource.Current;
                if (invoiceIncome.InvoiceIncomeID != 0)
                    root.ShowForm(new formProductIncomeDetail(invoiceIncome));
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            InvoiceIncome invoiceIncome = (InvoiceIncome)invoiceIncomeBindingSource.AddNew();
            if (gridInvoice.RowCount > 1)
                invoiceIncome.InvoiceIncomeNumber =
                        context.InvoiceIncomes.Max(x => x.InvoiceIncomeNumber) + 1;
            else invoiceIncome.InvoiceIncomeNumber = 1;
            invoiceIncome.Vat = Convert.ToInt32(context.Rates.Where(x => x.RateID == 1).Select(x => x.RateValue).First());
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            InvoiceIncome invoiceIncome = (InvoiceIncome)invoiceIncomeBindingSource.Current;
            if (invoiceIncome != null) db.DeleteInvoiceIncome(invoiceIncome);
            invoiceIncomeBindingSource.Remove(invoiceIncome);
            root.Submit();
        }
    }
}
